package com.miplus.ccrm.core.controller;

import com.miplus.ccrm.common.*;
import com.miplus.ccrm.core.controller.vo.credit.CrdCaseReqVo;
import com.miplus.ccrm.core.controller.vo.ficase.*;
import com.miplus.ccrm.core.security.JCommonData;
import com.miplus.ccrm.core.service.collect.*;
import lombok.extern.slf4j.Slf4j;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletResponse;
import javax.validation.constraints.NotNull;
import java.util.List;

/**
 * 2-8.Fi案件公共服务接口
 */
@Slf4j
@CrossOrigin(origins = "*", maxAge = 3600)
@RestController
@RequestMapping("/fi")
public class FiController {
    @Resource
    private JCommonData jCommonData;
    @Resource
    private FiCreditcasesHistService fiCreditcasesHistService;
    @Resource
    private FiImportHistService fiImportHistService;
    @Resource
    private FiTaskOldhistService fiTaskOldhistService;
    @Resource
    private FiTaskService fiTaskService;
    @Resource
    private FiCreditcasesService fiCreditcasesService;
    @Resource
    private FiNavigateSetService navigateSetService;

    /**
     * 取今日预约案件GRID
     */
    @GetMapping("/fiAppointCaseList")
    public R<PageData<FiAppointResVo>> fiAppointCaseList(@Validated FiAppointReqVo fiAppointReqVo){
        String usrid= jCommonData.getCurrUsrID();
        return R.SuccessPage(fiCreditcasesService.fiAppointCaseList(usrid,fiAppointReqVo));
    }

    /**
     * 取组对应案件数LIST
     */
    @PostMapping("/grpCaseCountList")
    public R<List<GrpCaseCountVo>> grpCaseCountList(@RequestBody @Validated GrpListReqVo grpListReqVo){
        return R.Success(fiCreditcasesService.grpCaseCountList(grpListReqVo));
    }

    /**
     * 取分案历史GRID
     */
    @GetMapping("/fiCaseDistList")
    public R<PageData<DistHistResVo>> fiCaseDistList(@Validated DistHistReqVo distListReqVo){
        JSysUser user= jCommonData.getCurrUsr();
        return R.SuccessPage(fiCreditcasesHistService.fiCaseDistList(user,distListReqVo));
    }

    /**
     * 取分案历史明细GRID
     */
    @GetMapping("/fiCaseDistDetailList")
    public R<PageData<FiDistDetailResVo>> fiCaseDistDetailList(@Validated FiDistDetailReqVo distDetailListReqVo){
        JSysUser user= jCommonData.getCurrUsr();
        return R.SuccessPage(fiCreditcasesHistService.fiCaseDistDetailList(user,distDetailListReqVo));
    }

    /**
     * 取分案批次明细（按机构）
     */
    @GetMapping("/fiCaseDistBatchDetailList")
    public R<PageData<DistHistResVo>> fiCaseDistBatchDetailList(@Validated FiDistBtcTailReqVo distGrpBtcTailReqVo){
        return R.SuccessPage(fiCreditcasesHistService.fiCaseDistBatchDetailList(distGrpBtcTailReqVo));
    }

    /**
     * 佣金试算
     */
    @PostMapping("/fiCommisList")
    public R<PageData<CommisResVo>> fiCommisList(@RequestBody @Validated CommisReqVo commisReqVo){
        JSysUser user= jCommonData.getCurrUsr();
        if(JPubUtils.IsEmpty(commisReqVo.getDistbatchid())&&JPubUtils.IsEmpty(commisReqVo.getGrpid())){
            return R.Success();
        }
        return R.SuccessPage(fiCreditcasesHistService.fiCommisList(user,commisReqVo));
    }

    /**
     * 佣金逾期档次结算
     */
    @PostMapping("/fiCommisMthsList")
    public R<PageData<CommisResVo>> fiCommisMthsList(@RequestBody @Validated CommisReqVo commisReqVo){
        JSysUser user= jCommonData.getCurrUsr();
        return R.SuccessPage(fiCreditcasesHistService.fiCommisMthsList(user,commisReqVo));
    }

    /**
     * 佣金批次结算明细
     */
    @PostMapping("/fiCommisTailList")
    public R<PageData<CmsTailResVo>> fiCommisTailList(@RequestBody @Validated CmsTailReqVo cmsTailReqVo){
        JSysUser user= jCommonData.getCurrUsr();
        return R.SuccessPage(fiCreditcasesHistService.fiCommisTailList(user,cmsTailReqVo));
    }

    /**
     * 佣金逾期档次结算导出
     */
    @PostMapping("/expCommisMthsList")
    public R<CmdResVo> expCommisMthsList(@RequestBody @Validated CommisReqVo commisReqVo, HttpServletResponse response){
        JSysUser user= jCommonData.getCurrUsr();
        return R.Success(fiCreditcasesHistService.saveMthsAsExcel(user,commisReqVo,response));
    }

    /**
     * 佣金批次结算明细导出
     */
    @PostMapping("/expCommisTailList")
    public R<CmdResVo> expCommisTailList(@RequestBody @Validated CmsTailReqVo cmsTailReqVo, HttpServletResponse response){
        JSysUser user= jCommonData.getCurrUsr();
        return R.Success(fiCreditcasesHistService.saveTailAsExcel(user,cmsTailReqVo,response));
    }

    /**
     * 取批次导入/导出历史GRID
     */
    @GetMapping("/fiImportBatchList")
    public R<PageData<FiImpResVo>> fiImportBatchList(@Validated FiImpReqVo fiImpReqVo){
        return R.SuccessPage(fiImportHistService.fiImportBatchList(fiImpReqVo));
    }

    /**
     * 写入导入导出记录
     */
    @PostMapping("/addImportHist")
    public R<CmdResVo> addImportHist(@RequestBody @Validated FiImpAddVo addVo){
        String usrid= jCommonData.getCurrUsrID();
        int ret=fiImportHistService.AddExportHist(
                addVo.getBatchid(),
                usrid,
                addVo.getTempid(),
                addVo.getFilename(),
                addVo.getOkcount()
        );
        return R.Success(new CmdResVo(ret,addVo.getBatchid()));
    }

    /**
     * 取催记记录GRID
     */
    @PostMapping("/taskHist")
    public R<PageData<FiTaskResVo>> taskHist(@RequestBody @Validated FiTaskReqVo fiTaskReqVo){
        JSysUser user= jCommonData.getCurrUsr();
        return R.SuccessPage(fiTaskService.taskHist(user,fiTaskReqVo));
    }

    /**
     * 催记历史导出
     */
    @PostMapping("/expTaskHist")
    public R<CmdResVo> expTaskHist(@RequestBody @Validated FiTaskReqVo fiTaskReqVo, HttpServletResponse response){
        JSysUser user= jCommonData.getCurrUsr();
        return R.Success(fiTaskService.saveAsExcel(user,fiTaskReqVo,response));
    }

    /**
     * 增加外呼催记
     */
    @PostMapping("/addDialTask")
    public R<CmdResVo> addDialTask(@RequestBody @Validated TaskCallAddVo taskCallAddVo){
        String usrid= jCommonData.getCurrUsrID();
        return R.Success(fiTaskService.addDialTask(usrid,taskCallAddVo));
    }

    /**
     * 保存外呼结果催记
     */
    @PostMapping("/saveDialTask")
    public R<CmdResVo> saveDialTask(@RequestBody @Validated TaskCallSaveVo taskCallSaveVo){
        String usrid= jCommonData.getCurrUsrID();
        fiCreditcasesService.updateCallTask(
                taskCallSaveVo.getFicaseid(),
                taskCallSaveVo.getTasknumber(),
                taskCallSaveVo.getAppointdt()
        );
        return R.Success(fiTaskService.saveDialTask(usrid,taskCallSaveVo));
    }

    /**
     * 增加短信催记
     */
    @PostMapping("/addSmsTask")
    public R<CmdResVo> addSmsTask(@RequestBody @Validated TaskSmsAddVo taskSmsAddVo){
        String usrid= jCommonData.getCurrUsrID();
        return R.Success(fiTaskService.addSmsTask(usrid,taskSmsAddVo));
    }

    /**
     * 保存短信发送结果催记
     */
    @PostMapping("/saveSmsTask")
    public R<CmdResVo> saveSmsTask(@RequestBody @Validated TaskSmsSaveVo taskSmsSaveVo){
        String usrid= jCommonData.getCurrUsrID();
        return R.Success(fiTaskService.saveSmsTask(usrid,taskSmsSaveVo));
    }

    /**
     * 查询历史催记Grid
     */
    @PostMapping("/viewHistTask")
    public R<PageData<HistTaskResVo>> viewHistTask(@RequestBody @Validated HistTaskReqVo histTaskReqVo){
        return R.SuccessPage(fiTaskOldhistService.viewHistTask(histTaskReqVo));
    }

    /**
     * 历史催记导出
     */
    @PostMapping("/expHistTask")
    public R<CmdResVo> expHistTask(@RequestBody @Validated HistTaskReqVo histTaskReqVo, HttpServletResponse response){
        String usrid= jCommonData.getCurrUsrID();
        return R.Success(fiTaskOldhistService.expHistTask(usrid,histTaskReqVo,response));
    }

    /**
     * 取案件当前进行的催记状态
     */
    @GetMapping("/getCurrentTask")
    public R<List<FiTaskCurrVo>> getCurrentTask(@RequestParam @NotNull Integer ficaseid){
        return R.Success(fiTaskService.getCurrentTask(ficaseid));
    }

    /**
     * 催记置顶
     */
    @PostMapping("/fiTaskTopSet")
    public R<CmdResVo> fiTaskTopSet(@RequestBody @Validated FiTaskTopVo fiTaskTopVo){
        return R.Success(fiTaskService.taskTopSet(fiTaskTopVo.getTasknumber(),fiTaskTopVo.getToppos()));
    }

    /**
     * 催记置顶取消
     */
    @PostMapping("/fiTaskTopCancel")
    public R<CmdResVo> fiTaskTopCancel(@RequestBody @Validated FiTaskTopVo fiTaskTopVo){
        return R.Success(fiTaskService.taskTopCancel(fiTaskTopVo.getTasknumber()));
    }

    /**
     * 导入离线催记
     */
    @PostMapping("/impTaskList")
    public R<ImpResultVo> impTaskList(@RequestBody @Validated FiTaskImpVo fiTaskImpVo){
        String usrid= jCommonData.getCurrUsrID();
        return R.Success(fiTaskService.impTaskList(usrid,fiTaskImpVo));
    }

    /**
     * 导入历史催记
     */
    @PostMapping("/impHistTaskList")
    public R<ImpResultVo> impHistTaskList(@RequestBody @Validated HistTaskImpVo histTaskImpVo){
        String usrid= jCommonData.getCurrUsrID();
        return R.Success(fiTaskService.impHistTaskList(usrid,histTaskImpVo));
    }

    /**
     * 导入催收函/诉讼/支付令
     */
    @PostMapping("/impTaskCmdList")
    public R<ImpResultVo> impTaskCmdList(@RequestBody @Validated TaskCmdImpVo taskCmdImpVo){
        String usrid= jCommonData.getCurrUsrID();
        return R.Success(fiTaskService.impTaskCmdList(usrid,taskCmdImpVo));
    }

    /**
     * 删除导入数据批次
     */
    @PostMapping("/delImpTaskList")
    public R<CmdResVo> delImpTaskList(@RequestBody @Validated TaskBatchDelVo taskBatchDelVo){
        return R.Success(fiTaskService.delImpTaskList(taskBatchDelVo));
    }

    /**
     * 取客户关联的案件数量
     */
    @GetMapping("/getCustCaseCount")
    public R<CmdResVo> getCustCaseCount(@RequestParam @NotNull String custnumber){
        return R.Success(fiCreditcasesService.getCustFiCaseCount(custnumber));
    }

    /**
     * 取案件下的减免申请数量
     */
    @GetMapping("/getFiCaseReliefCount")
    public R<CmdResVo> getFiCaseReliefCount(@RequestParam @NotNull Integer ficaseid){
        return R.Success(fiCreditcasesService.getFiCaseReliefCount(ficaseid));
    }

    /**
     * 取案件产品类型
     */
    @GetMapping("/getCrdProdType")
    public R<CmdResVo> getCrdProdType(@RequestParam @NotNull Integer ficaseid){
        return R.Success(fiCreditcasesService.getProdType(ficaseid));
    }

    /**
     * 取客户下的案件LIST.ID
     */
    @GetMapping("/getFiCaseIDList")
    public R<List<Integer>> getFiCaseIDList(@RequestParam @NotNull String custno){
        return R.Success(fiCreditcasesService.getCaseIDList(custno));
    }

    /**
     * 取一键回收数据进程
     */
    @GetMapping("/getRecycleProcess")
    public R<CmdResVo> getRecycleProcess(){
        return R.Success(fiCreditcasesService.getRecycleProcess());
    }

    /**
     * 取案件导航LIST
     */
    @GetMapping("/getCaseNavigate")
    public R<List<CaseNavResVo>> getCaseNavigate(){
        return R.Success(fiCreditcasesService.getCaseNavigate());
    }

    /**
     * 批次展期
     */
    @PostMapping("/fiCaseDistRoll")
    public R<CmdResVo> fiCaseDistRoll(@RequestBody @Validated DistRollVo distRollVo){
        String usrid= jCommonData.getCurrUsrID();
        return R.Success(fiCreditcasesService.fiCaseDistRoll(usrid,distRollVo));
    }

    /**
     * 取我的案件导航设置LIST
     */
    @GetMapping("/getNavigateList")
    public R<List<String>> getNavigateList(){
        String usrid= jCommonData.getCurrUsrID();
        return R.Success(navigateSetService.getNavigateList(usrid));
    }

    /**
     * 我的案件导航设置
     */
    @PostMapping("/setNavigateList")
    public R<CmdResVo> setNavigateList(@RequestBody List<String> mthsList){
        String usrid= jCommonData.getCurrUsrID();
        return R.Success(navigateSetService.setNavigateList(usrid,mthsList));
    }
}
